package com.zhongxi.guoyuan.utils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by Administrator on 2017/6/12.
 */

public class SpUtils {

    public static final String NEWID = "newid";
    private final String sp_name = "new_meirongyuan";
    private final SharedPreferences sp;

    private SpUtils() {
        sp = UiUtils.getContext().getSharedPreferences(sp_name, Context.MODE_PRIVATE);
    }

    private static SpUtils instance;

    public static SpUtils getInstance() {
        if (instance == null) {
            instance = new SpUtils();
        }
        return instance;
    }

    /**
     * 读取sp文件
     *
     * @param key
     * @return
     */
    public String readSp(String key) {
        return sp.getString(key, null);
    }

    /**
     * 写入sp文件
     *
     * @param key
     * @param value
     */
    public void saveSp(String key, String value) {
        sp.edit().putString(key, value).commit();
    }

    /**
     * 写入sp文件
     *
     * @param key
     * @param value
     */
    public void saveIntSp(String key, int value) {
        sp.edit().putInt(key, value).commit();
    }

    public void getNewId(String newId) {
        String oldId = readSp(NEWID);
        if (oldId == null) {
            oldId = "";
        }
        String spNewId = oldId + "#" + newId;
        saveSp(NEWID, spNewId);
    }
}
